package com.atguigu.dao;


import com.atguigu.util.JDBCUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;

public class BaseDao<T> {
    /**
     * 增删改操作
     * @param sql
     * @param params
     * @return
     */
    public boolean update(String sql,Object ...params) {
        //1.获取数据库的连接
        Connection connection = JDBCUtils.getConnection();
        //2.创建对象
        QueryRunner runner = new QueryRunner();
        //3接受传出来的sql语句
        //调用增删改方法
        try {
            int update = runner.update(connection, sql, params);
            if (update > 0) {
                return true;
            }
        } catch (SQLException e) {
            e.printStackTrace();
            //抛出运行时异常 让事务进行处理异常
            throw  new RuntimeException();
        } finally {
            //释放资源
           //JDBCUtils.releaseConnection();
        }
        return false;
    }

    /**
     * 查询所有的数据或者多条数据
     * @param type
     * @param sql
     * @param params
     * @return
     */
    public List<T>  getBeanList(Class type,String sql,Object ...params){
        //1.获取数据库的连接
        Connection connection = JDBCUtils.getConnection();
        //2.创建对象
        QueryRunner runner = new QueryRunner();
        //3执行
        try {
            return runner.query(connection,sql,new BeanListHandler<T>(type),params);
        } catch (SQLException e) {
            e.printStackTrace();
            //抛出运行时异常 让事务进行处理异常
            throw  new RuntimeException();
        }finally {
            //释放资源
          //  JDBCUtils.releaseConnection();
        }
    }

    /**
     * 查询一条数据
     * @param type
     * @param sql
     * @param params
     * @return
     */
    public T getBean(Class type,String sql,Object ...params){
        //1.获取数据库的连接
        Connection connection = JDBCUtils.getConnection();
        //2.创建对象
        QueryRunner runner = new QueryRunner();
        //执行
        try {
            return   runner.query(connection,sql,new BeanHandler<T>(type),params);
        } catch (SQLException e) {
            e.printStackTrace();
            //抛出运行时异常 让事务进行处理异常
            throw  new RuntimeException();
        }finally {
            //释放资源
           // JDBCUtils.releaseConnection();
        }
    }

    /**
     * 查询返回一个结果
     * @param type
     * @param sql
     * @param params
     * @return
     */
    public Object getObject(Class type,String sql,Object ...params){
        //1.获取数据库的连接
        Connection connection = JDBCUtils.getConnection();
        //2.创建对象
        QueryRunner runner = new QueryRunner();
        //执行
        try {
            return runner.query(connection,sql,new ScalarHandler(),params);
        } catch (SQLException e) {
            e.printStackTrace();
            //抛出运行时异常 让事务进行处理异常
            throw  new RuntimeException();
        }finally {
            //释放资源
          //  JDBCUtils.releaseConnection();
        }
    }
}
